package com.bstek.uflo.console.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.web.servlet.ModelAndView;

import com.bstek.uflo.process.assign.Assignee;
import com.bstek.uflo.process.assign.AssigneeProvider;
import com.bstek.uflo.process.assign.Entity;
import com.bstek.uflo.process.assign.PageQuery;

/**
 * @author Jacky.gao
 * @since 2013年9月10日
 */
public class AssigneeProviderController extends AbstractController implements InitializingBean {
	Map<String,AssigneeProvider> assigneeProviderMaps=new HashMap<String,AssigneeProvider>();
	@Override
	protected ModelAndView doHandleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception {
		preCheck();
		String pageIndex=request.getParameter("pageIndex");
		if(StringUtils.isEmpty(pageIndex))pageIndex="1";
		String pageSize=request.getParameter("pageSize");
		if(StringUtils.isEmpty(pageSize))pageSize="1000";
		String parentId=request.getParameter("parentId");
		if(parentId!=null && parentId.equals("null")){
			parentId=null;
		}
		String providerId=request.getParameter("providerId");
		PageQuery<Entity> pageQuery= new PageQuery<Entity>(Integer.valueOf(pageIndex),Integer.valueOf(pageSize));
		if(StringUtils.isNotEmpty(providerId)){
			AssigneeInfo info = buildAssigneeInfo(parentId, pageQuery,providerId,true);
			writeJson(response,info);
		}else{
			List<AssigneeInfo> result=new ArrayList<AssigneeInfo>();
			for(String beanId:assigneeProviderMaps.keySet()){
				AssigneeInfo info = buildAssigneeInfo(parentId, pageQuery,beanId,false);
				result.add(info);
			}
			writeJson(response,result);
		}
		return null;
	}
	
	private void writeJson(HttpServletResponse response,Object obj) throws IOException{
		response.setContentType("text/json");  
		response.setCharacterEncoding("UTF-8");  
		ObjectMapper mapper=new ObjectMapper();
		OutputStream out=response.getOutputStream();
		try{
			mapper.writeValue(out, obj);
		}finally{
			out.flush();
			out.close();
		}
	}

	private AssigneeInfo buildAssigneeInfo(String parentId,PageQuery<Entity> pageQuery, String beanId,boolean buildEntity) {
		AssigneeProvider provider=assigneeProviderMaps.get(beanId);
		provider.queryEntities(pageQuery, parentId);
		AssigneeInfo info=new AssigneeInfo();
		info.setName(provider.getName());
		info.setTree(provider.isTree());
		info.setProviderId(beanId);
		if(buildEntity){
			List<Assignee> assignees=new ArrayList<Assignee>();
			Collection<Entity> entitys=pageQuery.getResult();
			if(entitys!=null){
				for(Entity entity:entitys){
					Assignee assignee=new Assignee();
					assignee.setId(entity.getId());
					assignee.setName(entity.getName());
					assignee.setProviderId(beanId);
					assignee.setChosen(entity.isChosen());
					assignees.add(assignee);
				}
			}
			info.setAssignees(assignees);
			info.setCount(pageQuery.getRecordCount());
		}
		return info;
	}

	public void afterPropertiesSet() throws Exception {
		Map<String,AssigneeProvider> map=getApplicationContext().getBeansOfType(AssigneeProvider.class);
		ApplicationContext context=getApplicationContext().getParent();
		if(context!=null){
			map.putAll(context.getBeansOfType(AssigneeProvider.class));			
		}
		for(String beanId:map.keySet()){
			AssigneeProvider provider=map.get(beanId);
			if(!provider.disable()){
				assigneeProviderMaps.put(beanId, provider);
			}
		}
	}
}
